package com.ask.forMe.langchain4j.rag.splitter;

import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.Metadata;
import dev.langchain4j.data.segment.TextSegment;

import java.util.ArrayList;
import java.util.List;

/**
 * 正则表达式分割工具类1
 */
public class RegexSplitUtils {

    public static String[] splitByRegex(String text, String regex) {
        return text.split(regex);
    }

    /**
     * 字符分割
     *
     * @param text
     * @return
     */
    public static String[] splitByCharacter(String text) {
        return text.split("");
    }

    /**
     * 单词分割
     *
     * @param text
     * @return
     */
    public static String[] splitByWord(String text) {
        return text.split("\\s+");
    }

    /**
     * 行分割
     *
     * @param text
     * @return
     */
    public static String[] splitByLine(String text) {
        return text.split("\\s*\\R\\s*");
    }

    /**
     * 按多个换行符分割
     *
     * @param text
     * @param n
     * @return
     */
    public static String[] splitBylineBreak(String text, int n) {
        return text.split("\\R{" + n + ",}");
    }


    /**
     * 段落分割
     *
     * @param text
     * @return
     */
    public static String[] splitByParagraph(String text) {
        return text.split("\\s*(?>\\R)\\s*(?>\\R)\\s*");
    }


    /**
     * MarkDown标题分割
     *
     * @param text
     * @param minHeadingLevel
     * @param maxHeadingLevel
     * @return
     */
    public static String[] splitByHeading(String text, int minHeadingLevel, int maxHeadingLevel) {
        return text.split(generateHeadingPattern(minHeadingLevel, maxHeadingLevel));
    }

    /**
     * 按照固定分割符进行分割
     *
     * @param text
     * @param delimiter
     * @return
     */
    public static String[] splitByFixedDelimiter(String text, String delimiter) {
        return text.split(delimiter.trim());
    }


    /**
     * 将字符串数组转换为 TextSegment列表
     *
     * @param split
     * @param document
     * @return
     */
    public static List<TextSegment> splitsToSegments(String[] split, Document document) {
        List<TextSegment> segments = new ArrayList<>();
        int index = 0;
        for (String s : split) {
            if (s != null && !s.trim().isEmpty()) {
                Metadata metadata = document.metadata().copy().put("index", String.valueOf(index++));
                segments.add(TextSegment.from(s, metadata));
            }
        }
        return segments;
    }


    /**
     * 生成标题匹配规则
     *
     * @param minHeadingLevel
     * @param maxHeadingLevel
     * @return
     */
    public static String generateHeadingPattern(int minHeadingLevel, int maxHeadingLevel) {
        StringBuilder patternBuilder = new StringBuilder();
        patternBuilder.append("(?=(?m)^"); // 添加正向预查

        if (minHeadingLevel == maxHeadingLevel) {
            patternBuilder.append("#{").append(minHeadingLevel).append("}\\s+");
        } else {
            patternBuilder.append("#{").append(minHeadingLevel).append(",")
                    .append(maxHeadingLevel).append("}\\s+");
        }
        patternBuilder.append(")"); // 关闭正向预查
        return patternBuilder.toString();
    }
}
